<template>
  <div class="trade-progress-container">
    <el-tooltip v-if="tooltip" :content="`净买入额: ${formatAmount(netBuyAmount)}`" placement="top">
      <div class="trade-progress">
        <div class="progress-bar">
          <div class="buy-bar" :style="{ width: buyPercentage + '%' }"></div>
          <div class="sell-bar" :style="{ width: sellPercentage + '%' }"></div>
        </div>
        <div v-if="showLabels" class="progress-labels">
          <span class="buy-label">买入 {{ formatAmount(buyAmount) }}</span>
          <span class="sell-label">卖出 {{ formatAmount(sellAmount) }}</span>
        </div>
      </div>
    </el-tooltip>
    <div v-else class="trade-progress">
      <div class="progress-bar">
        <div class="buy-bar" :style="{ width: buyPercentage + '%' }"></div>
        <div class="sell-bar" :style="{ width: sellPercentage + '%' }"></div>
      </div>
      <div v-if="showLabels" class="progress-labels">
        <span class="buy-label">买入 {{ formatAmount(buyAmount) }}</span>
        <span class="sell-label">卖出 {{ formatAmount(sellAmount) }}</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'

const props = defineProps({
  buyAmount: { type: [Number, String], default: 0 },
  sellAmount: { type: [Number, String], default: 0 },
  netBuyAmount: { type: [Number, String], default: 0 },
  tooltip: { type: Boolean, default: true },
  showLabels: { type: Boolean, default: true },
})

const toNumber = (v) => {
  const n = Number(v)
  return isNaN(n) ? 0 : n
}

const buy = computed(() => Math.abs(toNumber(props.buyAmount)))
const sell = computed(() => Math.abs(toNumber(props.sellAmount)))
const total = computed(() => buy.value + sell.value)

const buyPercentage = computed(() => (total.value === 0 ? 0 : (buy.value / total.value) * 100))
const sellPercentage = computed(() => (total.value === 0 ? 0 : (sell.value / total.value) * 100))

const formatAmount = (amount) => {
  const num = toNumber(amount)
  if (Math.abs(num) >= 10000) {
    return (num / 10000).toFixed(1) + '万'
  }
  return num.toLocaleString('zh-CN')
}
</script>

<style scoped>
.trade-progress-container {
  width: 100%;
  padding: 4px 0;
}

.trade-progress {
  width: 100%;
}

.progress-bar {
  position: relative;
  width: 100%;
  height: 8px;
  background-color: #f0f0f0;
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 4px;
}

.buy-bar {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  background: linear-gradient(90deg, #f78989, #f56c6c);
  border-radius: 4px 0 0 4px;
  transition: width 0.3s ease;
}

.sell-bar {
  position: absolute;
  right: 0;
  top: 0;
  height: 100%;
  background: linear-gradient(90deg, #67c23a, #85ce61);
  border-radius: 0 4px 4px 0;
  transition: width 0.3s ease;
}

.progress-labels {
  display: flex;
  justify-content: space-between;
  font-size: 10px;
  color: #666;
}

.buy-label {
  color: #f56c6c;
  font-weight: 500;
}

.sell-label {
  color: #67c23a;
  font-weight: 500;
}

.trade-progress-container:hover .progress-bar {
  transform: scale(1.05);
  transition: transform 0.2s ease;
}

.trade-progress-container:hover .buy-bar,
.trade-progress-container:hover .sell-bar {
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
</style>